package com.lc.analyze.controller;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.system.UserInfo;
import com.alibaba.fastjson2.JSONObject;
import com.study.common.entity.bo.IdAnalyzeBo;
import com.study.common.entity.vo.HumanFaceAnalyzeVo;
import com.study.common.enums.GenderEnum;
import com.study.common.res.ApiResult;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.DetectFaceAttributesRequest;
import com.tencentcloudapi.iai.v20200303.models.DetectFaceAttributesResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/9/4 15:23
 */
@Slf4j
@RestController
@RequestMapping("/analyse")
public class AnalyzeController {

    @Resource
    private IaiClient iaiClient;

    @PostMapping(value = "/faceAnalyze")
    public ApiResult faceAnalyze(@RequestBody String base64) {
        try {
            DetectFaceAttributesRequest req = new DetectFaceAttributesRequest();
            req.setImage(base64);
            req.setFaceAttributesType("Age,Gender");
            // 返回的resp是一个DetectFaceAttributesResponse的实例，与请求对象对应
            DetectFaceAttributesResponse resp = iaiClient.DetectFaceAttributes(req);
            // 输出json格式的字符串回包
            String jsonString = DetectFaceAttributesResponse.toJsonString(resp);
            log.info("人脸分析结果：{}", jsonString);
            JSONObject jsonResult = JSONObject.parseObject(jsonString);
            JSONObject faceDetailInfos = jsonResult.getJSONArray("FaceDetailInfos").getJSONObject(0);
            JSONObject faceDetailAttributesInfo = faceDetailInfos.getJSONObject("FaceDetailAttributesInfo");
            String age = faceDetailAttributesInfo.getString("Age");
            byte gender = faceDetailAttributesInfo.getJSONObject("Gender").getByte("Type");
            return ApiResult.success(new HumanFaceAnalyzeVo(gender == GenderEnum.MAN.getCode() ? "男" : "女", Integer.valueOf(age)));
        } catch (TencentCloudSDKException e) {
            log.info("人脸分析异常，{}", e.toString());
            return ApiResult.fail(e.toString());
        }
    }

    @PostMapping(value = "idNumberAnalyze")
    public ApiResult idNumberAnalyze(@RequestBody @Validated IdAnalyzeBo idAnalyzeBo) {
        if (!IdcardUtil.isValidCard(idAnalyzeBo.getIdCard())) {
            return ApiResult.fail(4025, "身份证验证不合法");
        }
        // 对系统用户信息进行更新操作...
        return ApiResult.success("用户身份信息验证成功");
    }

    /**
     * 身份证脱敏
     * @param idCard 原始身份证号码
     * @return 脱敏后的身份证号码
     */
    public static String hideIdCard(String idCard) {
        return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
    }

}
